package com.ruoyi.web.controller.inspection;
import cn.hutool.core.io.IoUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.config.oss.OssProperties;
import com.ruoyi.common.config.oss.OssTemplate;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.vo.InspectionPlanTaskToVO;
import com.ruoyi.system.domain.vo.PointProjectVO;
import com.ruoyi.system.entity.InspectionPoint;
import com.ruoyi.system.service.InspectionPlanService;
import com.ruoyi.system.service.InspectionPointService;
import com.ruoyi.system.service.InspectionRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * (InspectionPoint)表控制层
 * @since 2022-11-25 17:10:29
 */
@RestController
@RequestMapping("/inspectionPoint")
public class InspectionPointController extends BaseController {

    @Resource
    private InspectionPointService inspectionPointService;

    @Resource
    private InspectionPlanService inspectionPlanService;

    @Resource
    private InspectionRouteService inspectionRouteService;


    /**
     * 分页查询
     * @param inspectionPoint 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public TableDataInfo queryByPage(InspectionPoint inspectionPoint) {
        startPage();
        List<InspectionPoint> inspectionPoints = this.inspectionPointService.queryByPage(inspectionPoint);
        return getDataTable(inspectionPoints);
    }

    /**
     * 通过主键查询单条数据
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult queryById(@PathVariable("id") Integer id) {
        return AjaxResult.success(this.inspectionPointService.queryById(id));
    }

    /**
     * 新增数据
     * @param inspectionPoint 实体
     * @return 新增结果
     */
    @PostMapping("/create")
    public AjaxResult add(@RequestBody InspectionPoint inspectionPoint) throws Exception {
        return AjaxResult.success(this.inspectionPointService.insert(inspectionPoint));
    }

    /**
     * 编辑数据
     * @param inspectionPoint 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody InspectionPoint inspectionPoint) {
        return AjaxResult.success(this.inspectionPointService.update(inspectionPoint));
    }

    /**
     * 删除数据
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/delete")
    public AjaxResult deleteById(Integer id) {
        return AjaxResult.success(this.inspectionPointService.deleteById(id));
    }

    @GetMapping("/info")
    public AjaxResult getPointAndProjectById(Integer id) {
        PointProjectVO pointProjectVO = inspectionPointService.getPointAndProjectById(id);
        return AjaxResult.success(pointProjectVO);
    }

    @PostMapping("/updateStopCheck")
    public AjaxResult updateStopCheck(@RequestBody InspectionPoint inspectionPoint) {
/*        if (stopCheck == 1) {
            InspectionPoint byId = inspectionPointService.queryById(id);
            InspectionRoute inspectionRoute = inspectionRouteService.queryById(byId.getRouteId());
            InspectionPlan inspectionPlan = new InspectionPlan();
            inspectionPlan.setRouteId(inspectionRoute.getId());
            List<InspectionPlan> inspectionPlans = inspectionPlanService.queryByPage(inspectionPlan);
            if (CollUtil.isNotEmpty(inspectionPlans)) {
                return AjaxResult.error(500,"该点位的巡检路线有计划绑定，无法停检");
            }
        }*/
        inspectionPointService.updateStopCheck(inspectionPoint);
        return AjaxResult.success();
    }


    /**
     *  通过巡检点ID  获取巡检任务 InspectionPlanTaskToVO
     */
    @GetMapping("/getByIdtOTaskInfo")
    public AjaxResult getByIdtOTaskInfo(Integer id) {
        long userId = SecurityUtils.getUserId();
        //long userId = 99;
        List<InspectionPlanTaskToVO> taskToVOS = new ArrayList<>();
        List<InspectionPlanTaskToVO> list = inspectionPointService.getByIdtOTaskInfo(id);
        if(!CollectionUtils.isEmpty(list)){
            for (InspectionPlanTaskToVO inspectionPlanTaskToVO : list) {
                String planUserIds = inspectionPlanTaskToVO.getPlanUserIds();
                if (StringUtils.isNotEmpty(planUserIds)){
                    List<String> asList = Arrays.asList(planUserIds.split(","));
                    if (!CollectionUtils.isEmpty(asList)){
                        if (asList.contains(String.valueOf(userId))){
                            taskToVOS.add(inspectionPlanTaskToVO);
                        }
                    }
                }
            }
        }
        return AjaxResult.success(taskToVOS);
    }

    @Autowired
    private OssTemplate template;
    @Autowired
    private OssProperties ossProperties;



    @GetMapping("/getInfoRwm")
    public AjaxResult getInfoRwm() {
        //String[] ids = {"642","637","636","635","634","633","632","631","630","629","628","627","626","625","64","63","62","61","60","59","58","57","56","55","54","53","52","51","49","48","47","46","45","44","43","42","41","40","39","38","37","36","35","34","33","32","31","30","29","28","27","26","25","24","23","22"};
        String[] ids = {"642","22"};
        List<String> list = Arrays.asList(ids);
        String objectURL ="";
        if (!CollectionUtils.isEmpty(list)){
            for (String id : list) {
                JSONObject object = new JSONObject();
                object.put("id",Integer.valueOf(id));
                object.put("type", Constants.IN);
                String json = JSONUtil.toJsonStr(object);

                File file = new File(id + ".jpg");
                // File generate = QrCodeUtil.generate(id, 300, 300,type, file);
                File generate = QrCodeUtil.generate(json, 300, 300, file);
                InputStream in = null;
                try {
                    in = new FileInputStream(generate);
                    template.putObject(ossProperties.getBucketName(),id + ".jpg",in);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    IoUtil.close(in);
                    boolean delete = file.delete();
                    if (delete) {
                        objectURL = ossProperties.getAgency() + "/" + ossProperties.getBucketName() + "/" + id + ".jpg";
                        System.out.println(objectURL);
                       // System.out.println("删除临时二维码文件成功");
                    } else {
                        System.out.println("删除临时二维码文件失败");
                    }
                }
            }
        }
        return AjaxResult.success();
    }


}

